package com.example.jeong.yuejian10.Fragment;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.jeong.yuejian10.Activity.NewPostActivity;
import com.example.jeong.yuejian10.Adapter.PostListAdapter;
import com.example.jeong.yuejian10.Bean.PostBean;
import com.example.jeong.yuejian10.Listener.RetrofitListener;
import com.example.jeong.yuejian10.Model.PostModel;
import com.example.jeong.yuejian10.R;

import java.util.List;

//发帖页
public class PostFragment extends Fragment implements RetrofitListener<List<PostBean>> {

    private RecyclerView recyclerView;
    private View view = null;
    private TextView AddPost;
    private PostListAdapter postListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.tab04, container,false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rV_post);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(6);
        recyclerView.addItemDecoration(decoration);
        getData();
        initViews();
        initEvevnts();
        return view;
    }
    private void initViews(){
        AddPost = (TextView) view.findViewById(R.id.addpost);
    }
    private void initEvevnts(){
        AddPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),NewPostActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccess(List<PostBean> postBean) {
        postListAdapter = new PostListAdapter(PostFragment.this.getActivity(),postBean);
        recyclerView.setAdapter(postListAdapter);

    }

    @Override
    public void onFail() {

    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration{
        private int space;

        public SpacesItemDecoration(int space){
            this.space=space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if (parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }

    private void getData(){
        PostModel postModel=new PostModel();
        postModel.getPostlist(this);
    }

    @Override
    public void registerForContextMenu(View view) {
        super.registerForContextMenu(view);
    }
}
